﻿using Vlog.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vlog.Db;
using Vlog.DataModels;

namespace Vlog.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //Create
            ApplicationDbContext db = new ApplicationDbContext();
            db.Posts.Add(new Post() { Content = "This is a post", Title = "Test Post", Date = DateTime.Now });
            db.SaveChanges();
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";
            //Read
            ApplicationDbContext db = new ApplicationDbContext();
            List<Post> posts = db.Posts.Include("Comments").ToList();

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";
            //Update
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Post post = db.Posts.Where(x => x.Id == 3).FirstOrDefault();
                post.Title = "I'm Updated!";
                db.SaveChanges();
            }
            //Delete
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Post post = db.Posts.LastOrDefault();
                db.Posts.Remove(post);
                db.SaveChanges();
            }
            return View();
        }
    }
}